# TMUX 配置支持
# link this file as ~/.tmux.conf

# 解绑 <C-b> 设置 <C-a> 为默认
unbind C-b 
set -g prefix C-a
bind-key C-a send-prefix

# 重新加载配置
bind r source-file ~/.config/tmux/tmux.conf \; display '~/.config/tmux/tmux.conf sourced'

# -- general -------------------------------------------------------------------
setw -g xterm-keys on
set -s escape-time 10      # faster command sequences
set -sg repeat-time 600    # increase repeat timeout
set -s focus-events on
set -q -g status-utf8 on   # expect UTF-8 (tmux < 2.2)
setw -q -g utf8 on         # 默认 utf8
set -g history-limit 5000  # 历史记录
set -g mouse on            # 鼠标支持

set -g default-terminal "tmux-256color" # 支持 256color 以及 bold,italic
set -ga terminal-overrides ',xterm-256color:Tc'

# undercurl support
set -as terminal-overrides ',*:Smulx=\E[4::%p1%dm'
# underscore colours - needs tmux-3.0
set -as terminal-overrides ',*:Setulc=\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m'
# https://github.com/alacritty/alacritty/issues/3037
# set-option -ga terminal-override ',alacritty:Ms=\E]52;c;%p2%s\007'

# -- display -------------------------------------------------------------------
set -g base-index 1           # 窗口初始编号从 1 开始
setw -g pane-base-index 1     # 网格编号从 1 开始
setw -g automatic-rename on   # rename window to reflect current program
set -g renumber-windows on    # 自动重新排序,关闭窗口后
set -g status off             # 默认不显示状态栏 
set -g status-position bottom # 状态栏 top/bottom
set -g set-titles on          # 设定终端标题
set -g display-panes-time 800 # slightly longer pane indicators display time
set -g display-time 1000      # slightly longer status messages display time
set -g status-interval 10     # 10s 重新渲染状态栏 # redraw status line every 10 seconds

# activity
set -g monitor-activity on
set -g visual-activity off

# -- bind ----------------------------------------------------------------

bind -r e set-option status   # 切换状态栏是否显示

# -- Sessons ----------------
bind C-c new-session # 创建 session
bind C-f command-prompt -p find-session 'switch-client -t %%' # 查询 session标题
bind BTab switch-client -l  # move to last session

# ---- window
# , rename window
bind -r . command-prompt -I "#I" { swap-window -t "%%" } # . 与输入需要标签互换
bind -r C-j swap-window -t -1 \; select-window -t -1 # 向前交换标签
bind -r C-k swap-window -t +1 \; select-window -t +1 # 向后交换标签
bind -r C-h previous-window # p 切换到上一个 window
bind -r C-l next-window     # n 切换到下一个 window 
bind Tab last-window        # 切换到最后

# ----- pane
# 创建网格快捷键，-c 使用当前目录
unbind %
bind - split-window -h -c "#{pane_current_path}" # 水平分割
unbind '"'
bind _ split-window -v -c "#{pane_current_path}" # 垂直分割

# pane 导航
bind -r h select-pane -L  # 切换 left
bind -r j select-pane -D  # 切换 down
bind -r k select-pane -U  # 切换 up
bind -r l select-pane -R  # 切换 right
bind > swap-pane -D       # 向后交换位置
bind < swap-pane -U       # 向前交换位置

# pane 大小
bind -r H resize-pane -L 2
bind -r J resize-pane -D 2
bind -r K resize-pane -U 2
bind -r L resize-pane -R 2

bind -r m resize-pane -Z # 切换 pane 最大化


# bind 
# Toggle popup window with Alt-2
# bind-key -n -N 'Toggle popup window' A-2 if-shell -F '#{==:#{session_name},popup}' {
#     detach-client
# } {
#     display-popup -d "#{pane_current_path}" -xC -yC -w 80% -h 75% -E 'tmux attach-session -t popup || tmux new-session -s popup'
# }

# -- buffers -----------

# bind b list-buffers  # list paste buffers
# bind p paste-buffer  # paste from the top paste buffer
# bind P choose-buffer # choose which buffer to paste from

# -- vi-mode copy 模式
set-window-option -g mode-keys vi

bind-key -T copy-mode-vi 'v' send -X begin-selection # start selecting text with "v"
bind-key -T copy-mode-vi C-v send -X rectangle-toggle
bind-key -T copy-mode-vi 'y' send -X copy-selection # copy text with "y"

unbind -T copy-mode-vi MouseDragEnd1Pane # don't exit copy mode after dragging with mouse

# --- Tmux Plugin Manager ---------------------------
# https://github.com/tmux-plugins/tpm

set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect' # persist tmux sessions after computer restart
set -g @plugin 'tmux-plugins/tmux-continuum' # automatically saves sessions for you every 15 minutes
set -g @plugin 'christoomey/vim-tmux-navigator' # for navigating panes and vim/nvim with Ctrl-hjkl

set -g @plugin 'jimeh/tmux-themepack' # to configure tmux theme
set -g @themepack 'powerline/default/cyan' # use this theme for tmux
# # set -g @themepack 'basic' # use this theme for tmux

# # theme
# set -g @plugin 'catppuccin/tmux'
# set -g @catppuccin_flavour 'latte' # or frappe, macchiato, mocha
# set -g @resurrect-capture-pane-contents 'on' # allow tmux-ressurect to capture pane contents
# set -g @continuum-restore 'on' # enable tmux-continuum functionality

# # Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.config/tmux/plugins/tpm/tpm'

# install:  <prefix I>
# uninstall: <prefix alt-u>
